package terra.ui.wnd;

import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Shape;
import org.newdawn.slick.gui.ComponentListener;
import org.newdawn.slick.gui.GUIContext;

import terra.ui.GUI;
/** Абстрактный класс, описывающий окно, изменяющее свой размер */
public abstract class ResizableWindow extends AbstractWindow {

	private int minWidth,maxWidth;
	private int minHeight,maxHeight;
	private Shape resizeShape;
	private boolean overResizeShape = false;
	private boolean allowHorisontalResize = true;
	private boolean allowVerticalResize = true;
	
	public ResizableWindow(GUIContext container, ComponentListener listener) {
		super(container, listener);
	}

	protected abstract void resized();
	
	@Override
	public void setLocation(int x, int y)
	{
		super.setLocation(x, y);
		if (resizeShape != null)
			resizeShape.setLocation(getX()+getWidth()-resizeShape.getWidth(), getY()+getHeight()-resizeShape.getHeight());
	
	}
	public void resize(int diffX,int diffY)	{
		
		int oldW = getWidth();
		int oldH  = getHeight();
		
		int newW = oldW + diffX;
		int newH = oldH+diffY;
		if ((newW >= getMinWidth() && newW <= getMaxWidth()) && isAllowHorisontalResize())			
			setArea(new Rectangle(getX(),getY(),newW,oldH));
		if ((newH >= getMinHeight() && newH <= getMaxHeight()) && isAllowVerticalResize())			
			setArea(new Rectangle(getX(),getY(),newW,oldH));		
		resizeShape.setLocation(getX()+getWidth()-resizeShape.getWidth(), getY()+getHeight()-resizeShape.getHeight());
		overTime = System.currentTimeMillis();
	}
	
	@Override
	public void mouseDragged(int oldx, int oldy, int newx, int newy) {
		if (!isVisible())
			return;
		if (overResizeShape && GUI.hasFocus() == this)
			resize(newx-oldx,newy-oldy);
		else
			super.mouseDragged(oldx, oldy, newx, newy);
	}
	@Override
	public void mouseMoved(int oldx, int oldy, int newx, int newy) {
		if (!isVisible())
			return;
		super.mouseMoved(oldx, oldy, newx, newy);
		overResizeShape = resizeShape.contains(newx, newy);
	}
	
	public int getMinWidth() {
		return minWidth;
	}

	public void setMinWidth(int minWidth) {
		this.minWidth = minWidth;
	}

	public int getMaxWidth() {
		return maxWidth;
	}

	public void setMaxWidth(int maxWidth) {
		this.maxWidth = maxWidth;
	}

	public int getMinHeight() {
		return minHeight;
	}

	public void setMinHeight(int minHeight) {
		this.minHeight = minHeight;
	}

	public int getMaxHeight() {
		return maxHeight;
	}

	public void setMaxHeight(int maxHeight) {
		this.maxHeight = maxHeight;
	}

	public Shape getResizeShape() {
		return resizeShape;
	}

	public void setResizeShape(Shape resizeShape) {
		this.resizeShape = resizeShape;
	}

	public boolean isAllowHorisontalResize() {
		return allowHorisontalResize;
	}

	public void setAllowHorisontalResize(boolean allowHorisontalResize) {
		this.allowHorisontalResize = allowHorisontalResize;
	}

	public boolean isAllowVerticalResize() {
		return allowVerticalResize;
	}

	public void setAllowVerticalResize(boolean allowVerticallResize) {
		this.allowVerticalResize = allowVerticallResize;
	}

	
	
}
